﻿using CloudDcPc.Entity;
using CloudDcPc.util;
using CloudDcPc.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CloudDcPc
{
    /// <summary>
    /// ShowOrderDetailDlg.xaml 的交互逻辑
    /// </summary>
    public partial class ShowOrderDetailDlg : Window
    {
        private List<string> mFood_type = new List<string>();
        private List<string> order_nums = new List<string>();
        private CheckOut co = null;
        private Order order = null;
        private CheckOutGz cogz = null;
        public ShowOrderDetailDlg(string order_num, string checkout_num)
        {
            InitializeComponent();
            txt_checkout_num.Text = checkout_num;

            try
            {
                order_nums.Add(order_num);
                co = App.CheckOutBean.GetCheckOut(order_num);
                cogz = App.CheckOutGzBean.GetGz(order_num);
                order = App.OrderBean.GetHisOrderByOrderNum(order_num);
                mFood_type = App.OrdersBean.GetHisTypeByOrderNum(order_nums);  //食物种类

                if (order == null)
                {
                    MessageBox.Show("未找到C" + checkout_num.Substring(1) + "的相关菜单明细！可能该单不在历史库，或桌位或服务员已被删除。");
                    //this.Close();
                    print.Visibility = Visibility.Collapsed;
                }
                else
                {

                    if (order.Together_name != "")
                    {
                        gb_tg.Header = order.Together_name + "(" + App.TogetherBean.GetTogetherByField("name", order.Together_name).Price + "元)";
                        DataTable tg_table = App.TogetherBean.GetOrdersByName(order.Together_name);
                        dg_tg.ItemsSource = tg_table.DefaultView;
                    }
                    else
                    {
                        gb_tg.Visibility = Visibility.Collapsed;
                    }

                    food_type_sp.Children.Clear();
                    //循环显示食物详情
                    for (int i = 0; i < mFood_type.Count; i++)
                    {
                        DataTable dt = App.OrdersBean.GetHisOrdersDataTableByOrderNumAndType(order_nums, mFood_type[i]);

                        Expander expander = new Expander();
                        expander.Header = mFood_type[i] + "(" + dt.Rows.Count + "项," + App.OrdersBean.GetHisAllPriceByOrderNumOrType(order_nums, mFood_type[i]) + "元)";
                        expander.IsExpanded = true;

                        DataGrid dg = new DataGrid();
                        dg.FontSize = 15;
                        dg.Margin = new Thickness(10, 5, 10, 10);
                        dg.IsReadOnly = true;
                        dg.ItemsSource = dt.DefaultView;

                        expander.Content = dg;

                        food_type_sp.Children.Add(expander);
                    }
                    //被退食物
                    DataTable table = App.OrdersBean.GetHisReturnedOrdersDataTableByOrderNum(order_nums);
                    //如果有被退食物就显示相关金额
                    if (table.Rows.Count > 0)
                    {
                        txt_returned_sum.Text = App.OrdersBean.GetHisReturnedOrdersSum(order_nums).ToString();
                        sp_returned.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        sp_returned.Visibility = Visibility.Collapsed;
                    }
                    dg_returnedorders.ItemsSource = table.DefaultView;


                    float all_final_price = App.OrdersBean.GetAllPriceByOrderNumOrType(order_nums, "");
                    //保存全局总价格
                    txt_sum.Text = co.Final_cash + "";
                    if (co.Final_cash == 0)
                    {
                        zj.Visibility = Visibility.Collapsed;
                    }
                }
            }
            catch (Exception eee)
            {
                MessageBox.Show("打开出错！错误信息：\n" + eee.Message + "\n" + eee.Source + "\n" + eee.StackTrace);
                return;
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            StringUtil strUtil = new StringUtil();
            string str = "";
            str += "账单号：" + co.Checkout_num + "\n";
            str += "服务员：" + App.UserBean.GetUserByField("user_num", order.User_num) + "\t\t人数:" + order.Size + "\n";
            str += "桌位：";

                str += App.TabBean.GetTabByField("tab_num", co.Tab_num + "").Name + " ";
  
            str += "\n\n" + strUtil.FillStr("品名", 6, "　") + strUtil.FillStr("数量", 3, "　") + strUtil.FillStr("单位", 3, "　") + strUtil.FillStr("单价", 4, "　") + strUtil.FillStr("总额", 4, "　") + "\n";
            str += "------------------------------------------\n";
            if (order.Together_name != "")
            {
                str += "套餐：" + order.Together_name + "(" + App.TogetherBean.GetTogetherByField("name", order.Together_name).Price + "元)\n";
                DataTable tg_table = App.TogetherBean.GetOrdersByName(order.Together_name);
                for (int i = 0; i < tg_table.Rows.Count; i++)
                {
                    str += strUtil.FillStr(tg_table.Rows[i]["品名"].ToString(), 8, "　");
                    str += strUtil.FillStr(tg_table.Rows[i]["数量"].ToString(), 3, "　");
                    str += strUtil.FillStr(tg_table.Rows[i]["单位"].ToString(), 3, "　");
                    str += strUtil.FillStr(tg_table.Rows[i]["单价（元）"].ToString(), 5, "　");

                    str += "\n";
                }
                str += "------------------------------------------\n";
            }
            DataTable dt = App.OrdersBean.GetHisOrdersDataTableByOrderNum(order_nums);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                str += strUtil.FillStr(dt.Rows[i]["品名"].ToString(), 7, "　");
                str += strUtil.FillStr(dt.Rows[i]["数量"].ToString(), 3, "　");
                str += strUtil.FillStr(dt.Rows[i]["单位"].ToString(), 3, "　");
                str += strUtil.FillStr(dt.Rows[i]["单价（元）"].ToString(), 5, "　");
                str += strUtil.FillStr(dt.Rows[i]["总额（元）"].ToString(), 4, "　");

                str += "\n";
            }
            DataTable returned_dt = App.OrdersBean.GetHisReturnedOrdersDataTableByOrderNum(order_nums);
            if (returned_dt.Rows.Count > 0)
            {
                for (int i = 0; i < returned_dt.Rows.Count; i++)
                {
                    str += "[退]" + returned_dt.Rows[i]["品名"].ToString().PadRight(12, ' ');
                    str += "-" + returned_dt.Rows[i]["数量"].ToString().PadRight(3, ' ');
                    str += "\n";
                }
            }
            if (dt.Rows.Count > 0)
                str += "------------------------------------------\n";
            str += "小\t计：" + co.Sum + " 元\n";
            str += "优惠：" + co.Sub_sum + " 元";
            if (co.Sub_sum > 0)
                str += "\t批准人：" + App.UserBean.GetUserByField("user_num", co.Approver);
            str += "\n";

            str += "应收金额：" + (co.Sum - co.Sub_sum) + " 元\n";

            
            if (cogz != null)
            {
                str += "挂账信息：" + cogz.Sign_name + "(" + cogz.Department + ")\n";
            }
            else
            {
                if (co.Cash_sum != 0)
                {
                    str += "现金：" + co.Cash_sum + "元\n";
                }
                if (co.Djq_sum != 0)

                    str += "代金券：" + co.Djq_sum + "元 ";
                if (co.Xyk_sum != 0)

                    str += "信用卡：" + co.Xyk_sum + "元 ";
                if (co.Hyk_sum != 0)

                    str += "会员卡：" + co.Hyk_sum + "元";

            }
            str += "\n----------------------------------------\n" + Properties.Settings.Default.save_print_foot + "\n.\n.\n.\n.\n";
            //MessageBox.Show(str);   //FORDEBUG
            ///开始打印
            bool print_flag = true;

            while (print_flag)
            {
                try
                {
                    PrintUtil printUtil = new PrintUtil();
                    printUtil.UsbPrintString(Properties.Settings.Default.save_print_title + "\n.\n"+str);
                    print_flag = false;
                }
                catch
                {
                    string messageBoxText1 = "打印出错，请检查打印机（是否正确连接等）\n点“是”重试";
                    string caption1 = "打印出错";
                    MessageBoxButton button1 = MessageBoxButton.YesNo;
                    MessageBoxImage icon1 = MessageBoxImage.Warning;
                    MessageBoxResult result1 = MessageBox.Show(messageBoxText1, caption1, button1, icon1);
                    if (result1 == MessageBoxResult.No)
                        print_flag = false;

                }
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
